package com.laoxu.breadcrumb.controller;

import com.laoxu.breadcrumb.entity.Category;
import com.laoxu.breadcrumb.service.CategoryService;
import com.laoxu.breadcrumb.util.Result;
import com.laoxu.breadcrumb.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 分类控制器
 * @Author laoxu
 * @Date 2019/10/5 10:16
 **/
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /**
     *  获取父类列表（面包屑）
     * @param id
     * @return
     */
    @GetMapping("/parentCategoryList")
    public Result<List<Category>> getMenu(@RequestParam("id") Long id) {
        List<Category> categories = categoryService.findAllById(id);

        return ResultUtil.ok(categories);
    }

    /**
     *  获取子类列表
     * @param parentId
     * @return
     */
    @GetMapping("/subCategoryList")
    public Result<List<Category>> getListByParentId(@RequestParam("parentId") Long parentId) {
        List<Category> categories = categoryService.findByParentId(parentId);

        return ResultUtil.ok(categories);
    }

    /**
     *  根据级别查找分类
     * @param level
     * @return
     */
    @GetMapping("/levelCategoryList")
    public Result<List<Category>> getListByLevel(@RequestParam("level") Integer level) {
        List<Category> categories = categoryService.findByLevel(level);

        return ResultUtil.ok(categories);
    }


}
